<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动画</title>
    <style>
        div {
            position: absolute;
            width: 100px;
            height: 100px;
            background-color: pink;
        }

        div:nth-child(2) {
            /* 动画得定位 */
            position: absolute;
            top: 130px;
            width: 100px;
            height: 100px;
            background-color: rgb(197, 192, 255);
        }
    </style>
</head>

<body>
    <button>pyy</button>
    <div>胡歌</div>
    <div id="s">pyy</div>
    <script>
        let div = document.querySelector('div')
        console.log(div.offsetLeft);
        let time = setInterval(function () {
            if (div.offsetLeft == 200) {
                clearInterval(time)
            } else {
                div.style.left = div.offsetLeft + 1 + 'px';
            }
        }, 10)
        // 封装动画
        let div1 = document.querySelector('#s')
        function a(o, m) {
            // 有个按钮就会一直触发所以触发前先清除之前的定时器就不会加速
            clearInterval(o.b)
             o.b = setInterval(c, 10)
            function c() {
                //    console.log(div1);
                if (o.offsetLeft == m) {
                    clearInterval(b)
                }
                o.style.left = o.offsetLeft + 1 + 'px'
            }
        }

      let btn = document.querySelector('button')
       btn.addEventListener('click',function(){
        a(div1, 500)
       }) 
    </script>
</body>

</html>